/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sequential;

import java.util.Arrays;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 *
 * @author zwirek
 */
public class CharArrPool {

//    static private Vector<char[]> pool;
    static private ConcurrentHashMap<String,char[]> map;

    public static char[] getCanonicalVersion(char[] str) {
//        if (pool == null) {
//            pool = new Vector<char[]>();
//        }
//        for (char[] c : pool) {
//            if(c.length!=str.length){
//                continue;
//            }
//            for (int i = 0; i < c.length; i++) {
//                if(c[i]!=str[i]){
//                    break;
//                }else{
//                    if(i==c.length-1){
//                        return c;
//                    }
//                }
//
//            }
//        }
//        pool.add(str);
//        return pool.lastElement();
////    if(map==null){
////        map = new ConcurrentHashMap<char[],char[]>();
////    }
////    char[] canon = map.putIfAbsent(str, str);
////    return (canon == null) ? str : canon;
    if(map==null){
        map = new ConcurrentHashMap<String,char[]>();
    }
    String s = new String(str);
    char[] canon = map.putIfAbsent(s, str);
    return (canon == null) ? str : canon;
    }
    }

